package com.guo.worktool.manager.factory.StrConvertFactory;

import com.alibaba.fastjson2.JSON;
import org.springframework.stereotype.Service;

@Service("map")
public class MapService implements StrConvertService{
    @Override
    public String convert(String convertStr) {
        String[] split = convertStr.split("\n");
        StringBuilder sb = new StringBuilder();
        if(split.length<1){
            return sb.toString();
        }
        String last = split[split.length - 1];
        if(!last.contains("-")){
            return "最后一行为变量命名,例如p1-s1";
        }
        String[] blName = last.split("-",2);
        String blN = blName[0];
        String blStr = blName[1];
        String prefix = "Map<String,String> zdyData  = Maps.newLinkedHashMap();\n";
        sb.append(prefix);
        int count = 0;
        for (String str : split) {
            if(str.contains(":")){
                String[] two = str.split(":",2);
                sb.append("zdyData.put(\"").append(two[0]).append("\",")
                        .append(JSON.toJSONString(two[1].trim())).append(");")
                        .append("\n");
                count++;
            }
        }
        sb.append("log.info(\"zdyData {}\",zdyData);").append("\n");
        sb.append("String ").append("zdyStr")
                .append("= CommentRequestUtil.requestXXXBackJSONObject(gjjWebClientTL.get(), \"\", zdyData);")
                .append("\n");
        sb.append("log.info(\"zdyStr {}\",zdyStr);").append("\n");
        sb.append("成功处理了").append(count).append("条");
        String returnStr = sb.toString();
        returnStr = returnStr.replace("zdyData",blN);
        returnStr = returnStr.replace("zdyStr",blStr);
        return returnStr;
    }
}
